package com.hkfn.rfb.common.fragment;


import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.adapter.BarFloorAdapter;
import com.hkfn.rfb.common.adapter.BaseRecyclerAdapter;
import com.hkfn.rfb.common.adapter.FragmentPagerAdapter;
import com.hkfn.rfb.common.bean.BannerModel;
import com.hkfn.rfb.common.bean.ConstData;
import com.hkfn.rfb.common.utils.DialogUtils;
import com.hkfn.rfb.common.view.InitView;
import com.hkfn.rfb.common.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class BarFloorFragment extends BaseFragment {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private RecyclerView rlv_title_bar;
    private LinearLayoutManager linearLayoutManager;
    private BarFloorAdapter barFloorAdapter;

    private NoScrollViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragments;
    private List<Fragment> webFragments;
    private int currentSelectPosition = -1;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private Dialog chooseFloorDialog;

    private String barStyle = "0";// 导航显示 0现场勘查 1设施实施 2我的工单 3TCP指令下发 4

    private boolean isFirstLoad = true;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            if (null != bundle){
                barStyle = bundle.getString(ConstData.BARTYPE, "0");
            }
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_common_bar_floor, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        initHeadView();
        initViewPager();

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        include_progress_bar.setVisibility(View.GONE);
        InitView.instance().initProgressBar(activity, include_progress_bar);

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_no_data.setVisibility(View.GONE);

        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setVisibility(View.GONE);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });

        List<BannerModel> bannerModels = new ArrayList<>();
        switch (barStyle) {

        }

        setFragments(bannerModels);
    }

    private void initHeadView() {
        rlv_title_bar = itemContentView.findViewById(R.id.rlv_title_bar);
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rlv_title_bar.setLayoutManager(linearLayoutManager);
        barFloorAdapter = new BarFloorAdapter(activity);
        rlv_title_bar.setAdapter(barFloorAdapter);
        barFloorAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<BannerModel>() {
            @Override
            public void onItemClick(View v, int position, BannerModel data) {
                viewPager.setCurrentItem(position);
            }
        });
    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        webFragments = new ArrayList<>();
        viewPager = itemContentView.findViewById(R.id.viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setScroll(true);
        fragmentPagerAdapter = new FragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentSelectPosition = position;
                barFloorAdapter.setCurrrentPosition(position);
                if (position > 2) {
                    linearLayoutManager.scrollToPositionWithOffset(position - 2, 0);
                } else {
                    linearLayoutManager.scrollToPosition(0);
                }
                if (null != fragments){
                    Fragment fragment = fragments.get(position);
                    if (fragment instanceof BaseFragment && ((BaseFragment)fragment).isLoaded()) {
                        ((BaseFragment) fragment).updateData();
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 现场勘查标题
     * @return
     */
    private Collection<? extends BannerModel> initAddressTitleBar() {
        List<BannerModel> bannerModels = new ArrayList<>();
        BannerModel bannerModel1 = new BannerModel();
        bannerModel1.setTitle("待提交");
        bannerModels.add(bannerModel1);
        BannerModel bannerModel2 = new BannerModel();
        bannerModel2.setTitle("被驳回");
        bannerModels.add(bannerModel2);
        BannerModel bannerModel3 = new BannerModel();
        bannerModel3.setTitle("待审核");
        bannerModels.add(bannerModel3);
        BannerModel bannerModel4 = new BannerModel();
        bannerModel4.setTitle("已通过");
        bannerModels.add(bannerModel4);
        return bannerModels;
    }

    /**
     * 设施实施标题
     * @return
     */
    private Collection<? extends BannerModel> initDeviceTitleBar() {
        List<BannerModel> bannerModels = new ArrayList<>();
        BannerModel bannerModel1 = new BannerModel();
        bannerModel1.setTitle("待提交");
        bannerModels.add(bannerModel1);
        BannerModel bannerModel2 = new BannerModel();
        bannerModel2.setTitle("已提交");
        bannerModels.add(bannerModel2);
        return bannerModels;
    }




    private void setFragments(final List<BannerModel> bannerModels) {
        if (null != bannerModels && bannerModels.size() > 0){
            include_no_data.setVisibility(View.GONE);
            currentSelectPosition = 0;

            fragments.clear();
            barFloorAdapter.clear();
            for (int i = 0; i < bannerModels.size(); i++){
                switch (barStyle){

                }
            }
            barFloorAdapter.addDatas(bannerModels);
            fragmentPagerAdapter.appendList(fragments);
            viewPager.setCurrentItem(0);
            barFloorAdapter.setCurrrentPosition(0);
        }else {
            include_no_data.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogUtils.closeDialog(chooseFloorDialog);
    }

    @Override
    public void updateData() {
        super.updateData();
        if (null != fragments){
            for (int i = 0; i <fragments.size(); i++){
                Fragment fragment = fragments.get(i);
                if (fragment instanceof BaseFragment && ((BaseFragment) fragment).isLoaded()) {
                    ((BaseFragment) fragment).updateData();
                }
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isFirstLoad){
            updateData();
        }
        isFirstLoad = false;
    }

}
